package cn.dada.dadarecipes.base.response;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class JsonResult {
    private Integer code; //状态码
    private String message; //响应信息
    private Object data; //具体数据

    /** 用自定义状态码代替构造器参数-无具体数据返回 */
    public JsonResult(StatusCode statusCode) {
        this.code = statusCode.getCode();
        this.message = statusCode.getMsg();
    }

    /** 用自定义状态码代替构造器参数-有具体数据返回 */
    public JsonResult(StatusCode statusCode,Object data) {
        this.code = statusCode.getCode();
        this.message = statusCode.getMsg();
        this.data = data;
    }


    /**
     * 操作成功的场景封装为静态方法
     * 1.有返回值
     * 2.无返回值
     */
    //有返回值
    public static JsonResult ok(Object data){
        return new JsonResult(StatusCode.OPERATION_SUCCESS,data);
    }
    //无返回值
    public static JsonResult ok(){
        return JsonResult.ok(null);
    }


    public static JsonResult fail(Object data){
        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }
    public static JsonResult fail(){
        return JsonResult.fail(null);
    }
}
